package com.ruoyi.chat.controller;

import com.ruoyi.chat.service.ICharService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.VideoUtil;
import com.ruoyi.userSpeechFeatures.domain.UserSpeechFeatures;
import com.ruoyi.userSpeechFeatures.service.IUserSpeechFeatures;
import com.ruoyi.web.controller.api.service.ApiService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @program: RuoYi-Vue-master
 * @description:
 * @author: xgw
 * @create: 2025-01-10 14:46
 **/


@RestController
@RequestMapping("/char")
public class CharController {

    @Autowired
    private ApiService apiService;

    @Autowired
    private IUserSpeechFeatures iUserSpeechFeatures;


    @Autowired
    private ICharService iCharService;

    @PostMapping(value = "/charWithVideo")
    public AjaxResult charWithVideo(@RequestParam("userId")String userId,
                                    @RequestParam("file")MultipartFile file){
        //1.判断参数
        if(StringUtils.isBlank(userId)){
            return AjaxResult.error("用户Id...");
        }
        //2.保存录音文件，得到上传路径
        String fileFinallyPath = null;
        try {
            fileFinallyPath= VideoUtil.saveVideo(userId,file);
        }catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("上传录音文件...");
        }
        //3.得到上传音频的文字内容
        String userSpeech = iCharService.charWithVideo(userId, fileFinallyPath);
        System.out.println("userSpeech:" + userSpeech);
        //4.调用模型的api接口
        return apiService.callExternalApi(userSpeech);


    }


    //录入语音，识别音线的方法
    @PostMapping(value = "/userSpeechFeaturesVideo",headers = "content-type=multipart/form-data")
    public AjaxResult userSpeechFeaturesVideo(@RequestParam("userId")Integer userId,@RequestParam("tos")String tos,
                                              @RequestParam("file")MultipartFile files[]){
        if(StringUtils.isBlank(tos)){
            return AjaxResult.error("传递的参数为...String tos, MultipartFile file, Integer flag ");
        }
        //保存录音文件地址
        List<String> videos = new ArrayList<>();
        try {
            for (MultipartFile file : files) {
                String uploadPath = VideoUtil.saveVideo(tos, file);
                videos.add(uploadPath);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //识别声线
        UserSpeechFeatures usf = new UserSpeechFeatures(null, null, userId);
        iUserSpeechFeatures.speechRecognitionFun(usf,videos);
        return AjaxResult.success("声线录入成功！");
    }

}
